package com.naiterui.ehp.bs.cms.modules.notice.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.naiterui.ehp.bp.security.utils.SecurityUtils;
import com.naiterui.ehp.bs.cms.common.vo.PageParamsVO;
import com.naiterui.ehp.bs.cms.common.vo.PageVO;
import com.naiterui.ehp.bs.cms.modules.notice.entity.NoticeMessage;
import com.naiterui.ehp.bs.cms.modules.notice.mapper.NoticeMessageMapper;
import com.naiterui.ehp.bs.cms.modules.notice.service.NoticeMessageService;
import com.naiterui.ehp.bp.bo.notice.NoticeMessageBO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author luao
 * @date 2021/3/9 5:11 下午
 **/
@Slf4j
@Service
@RequiredArgsConstructor
public class NoticeMessageServiceImpl implements NoticeMessageService {

    private final NoticeMessageMapper noticeMessageMapper;

    @Override
    public PageVO<NoticeMessageBO> list(PageParamsVO pageParamsVO, String title) {
        QueryWrapper<NoticeMessage> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StrUtil.isNotBlank(title), "title", title);
        queryWrapper.eq("type", NoticeMessage.TYPE_MANAGEMENT);
        queryWrapper.eq("del_flag", 0);
        queryWrapper.orderByDesc("release_time", "created_at");
        Page<NoticeMessageBO> page = noticeMessageMapper.getPage(pageParamsVO.getPage(), queryWrapper);
        return PageVO.toPageVo(page);
    }

    @Override
    public void saveOrUpdate(NoticeMessageBO noticeMessageVO) {
        NoticeMessage noticeMessage = null;
        if (noticeMessageVO.getId() != null) {
            noticeMessage = noticeMessageMapper.selectOne(
                    new QueryWrapper<NoticeMessage>().lambda()
                            .eq(NoticeMessage::getId, noticeMessageVO.getId())
                            .eq(NoticeMessage::getDelFlag , 0));
            if (noticeMessage == null) {
                log.error("查询不到当前通知消息 id:{}", noticeMessageVO.getId());
                return;
            }
        }
        String userName = SecurityUtils.getCurrentUsername();
        if (noticeMessage == null) {
            noticeMessage = new NoticeMessage();
            noticeMessage.setCreatedBy(userName);
            noticeMessage.setCreatedAt(new Date());
        }
        noticeMessage.setTitle(noticeMessageVO.getTitle());
        noticeMessage.setContent(noticeMessageVO.getContent());
        noticeMessage.setReleaseTime(noticeMessageVO.getReleaseTime());
        noticeMessage.setType(NoticeMessage.TYPE_MANAGEMENT);
        noticeMessage.setChangedBy(userName);
        noticeMessage.setChangedAt(new Date());
        if (noticeMessage.getId() == null) {
            noticeMessageMapper.insert(noticeMessage);
            return;
        }
        noticeMessageMapper.update(noticeMessage,
                new QueryWrapper<NoticeMessage>().lambda().eq(NoticeMessage::getId, noticeMessage.getId()));
    }

    @Override
    public void delete(Long id) {
        NoticeMessage noticeMessage = noticeMessageMapper.selectById(id);
        if (noticeMessage == null) {
            log.error("查询不到当前通知消息 id:{}", id);
            return;
        }
        noticeMessage.setDelFlag(1);
        noticeMessageMapper.update(noticeMessage, new QueryWrapper<NoticeMessage>().lambda().eq(NoticeMessage::getId, noticeMessage.getId()));
    }

}
